<template>
  <div class="form">
    <el-row :gutter="24">
      <el-col :xs="24" :sm="24" :md="16" :lg="12" :xl="8">
        <el-form :model="formData" ref="ruleForm" label-width="150px" class="demo-ruleForm" :hide-required-asterisk="true">
          <el-row :gutter="24">
            <el-col :xs="24">
              <el-form-item :label="$t('system.system_name')" prop="ssoSessionMaxLifespan" class="spread">
                <el-input v-model="formData.sysName" placeholder="" clearable></el-input>
              </el-form-item>
            </el-col>

            <el-col :xs="24">
              <el-form-item :label="$t('system.maximum_effective_time_of_the_session')" prop="ssoSessionMaxLifespan" class="spread">
                <el-select v-model="formData.ssoSessionMaxLifespan" placeholder="">
                  <el-option v-for="(item,index) in time" :key="index" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24">
              <el-form-item :label="$t('system.session_idle_timeout')" prop="ssoSessionIdleTimeout" class="spread">
                <el-select v-model="formData.ssoSessionIdleTimeout" placeholder="">
                  <el-option v-for="(item,index) in overtime" :key="index" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24">
              <el-form-item :label="$t('system.minimum_password_length')" prop="passwordLength" class="minLength">
                <el-input type="number" v-enterNumber v-model="formData.passwordLength" placeholder="" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24">
              <el-form-item :label="$t('system.password_requires_characteristics')" prop="passwordRules">
                <el-checkbox-group v-model="formData.passwordRules">
                  <el-checkbox label="1">{{$t('system.capital')}}</el-checkbox>
                  <el-checkbox label="2">{{$t('system.lowercase_letters')}}</el-checkbox>
                  <el-checkbox label="3">{{$t('system.number')}}</el-checkbox>
                  <el-checkbox label="4">{{$t('system.special_characters')}}</el-checkbox>
                </el-checkbox-group>
              </el-form-item>
            </el-col>
            <el-col :xs="24">
              <el-form-item>
                <el-button @click="handleReset('ruleForm')">{{$t('common.reset')}}</el-button>
                <el-button type="primary" @click="handleSave('ruleForm')" :loading="loading">{{ $t("common.preservation") }}</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { siteConfig } from '@/api/siteConfig'
export default {
  data () {
    return {
      formData: {
        sysName: '',
        ssoSessionMaxLifespan: '',
        ssoSessionIdleTimeout: '',
        passwordLength: '',
        passwordRules: []
      },
      time: [{
        label: "禁用",
        value: 315360000
      }, {
        label: "4小时",
        value: 14400
      }, {
        label: "8小时",
        value: 28800
      }, {
        label: "12小时",
        value: 43200
      }, {
        label: "24小时",
        value: 86400
      }, {
        label: "7天",
        value: 604800
      }, {
        label: "30天",
        value: 2592000
      }, {
        label: "365天",
        value: 31536000
      }],
      overtime: [{
        label: "禁用",
        value: 315360000
      }, {
        label: "30分钟",
        value: 1800
      }, {
        label: "1小时",
        value: 3600
      }, {
        label: "2小时",
        value: 7200
      }, {
        label: "4小时",
        value: 14400
      }, {
        label: "8小时",
        value: 28800
      }, {
        label: "12小时",
        value: 43200
      }, {
        label: "24小时",
        value: 86400
      }],
      loading: false
    }
  },
  created () {
    this.findBasicConfig()
  },
  methods: {
    findBasicConfig () {
      siteConfig.findBasicConfig().then(res => {
        if (res.code == 200) {
          res.data.passwordRules = res.data.passwordRules.split(',')
          this.formData = res.data
          console.log(this.formData)
        }
      })
    },
    handleSave (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const param = Object.assign({}, this.formData)
          param.passwordRules = this.formData.passwordRules.join(',')
          console.log(param)
          this.loading = true
          siteConfig.saveBasicConfig(param).then(res => {
            if (res.code == 200) {
              this.$message({
                message: '修改成功',
                type: 'success'
              });
            } else {
              this.$message.error(res.msg);
            }
            this.loading = false
          })
        } else {
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.spread >>> .el-select {
  width: 100%;
}
</style>